<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>直播课程</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link href="/node_modules/LobsterUIFrame/layuiadmin/layui/css/layui.css" rel="stylesheet" />
	<link href="/node_modules/LobsterUIFrame/layuiadmin/style/admin.css" rel="stylesheet" />
	<link href="/node_modules/LobsterUIFrame/layuiadmin/layui/css/modules/layui-icon-extend/iconfont.css"
		rel="stylesheet" />
	<script src="/node_modules/LobsterUIFrame/layuiadmin/layui/layui.js"></script>
</head>

<body>
	<div class="layui-fluid">
		<div class="layui-card">
			<div class="layui-form layui-card-header layuiadmin-card-header-auto">
				<div class="layui-input-inline">
					<label class="layui-form-label">课程名称：</label>
					<div class="layui-input-inline">
						<input type="text" name="CourseName" id="CourseName" placeholder="请输入名称查询" autocomplete="off"
							class="layui-input">
					</div>
				</div>
				<div class="layui-input-inline">
					<label class="layui-form-label">课程类型：</label>
					<div class="layui-input-inline">
						<select name="CategoryId" id="CategoryId" lay-filter="CategoryId">
							<option value="-1" selected="selected">全部</option>
						</select>
					</div>
				</div>
				<div class="layui-input-inline">
					<button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-app-front-search"
						id="btnSearch">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
					</button>
				</div>
			</div>

			<div class="layui-card-body">
				<script id="toolbar" type="text/html">
		                <div class="layui-btn-group">
		                    <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>
		                </div>
		            </script>
				<table id="dataTable" lay-filter="dataTable"></table>
			</div>
		</div>
	</div>

	<script type="text/html" id="flagTpl">
		    {{#  if(d.DelFlag == '1'){ }}
		    <span style="color:red;">停用</span>
		    {{#  } else { }}
		    启用
		    {{#  } }}
		</script>

	<script type="text/html" id="option">
		    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
			<a class="layui-btn layui-btn-xs" lay-event="selectDoc">授课医生</a>
		    {{#  if(d.DelFlag == '0'){ }}
		    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable"><i class="layui-icon layui-extend-stop"></i>停用</a>
		    {{#  } else { }}
		    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable"><i class="layui-icon layui-icon-ok"></i>启用</a>
		    {{#  } }}
		</script>
</body>

</html>

<script>
	layui.config({
		base: '/node_modules/LobsterUIFrame/layuiadmin/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	});

	layui.define(['index', 'layer', 'form', 'element', 'table'], function () {
		var layer = layui.layer;
		var form = layui.form;
		var element = layui.element;
		var table = layui.table;
		var $ = layui.$;
		var admin = layui.admin;
		var setter = layui.setter;


		var controller = {
			data: {
				datatable: null,
				datatableoptions: {},
				layerindex: 0,
				model: {}
			},
			//初始化页面
			initpage: function () {
				this.initevent();

				this.getlivecourse();
			},
			getbaseurl: function (fun) {
				var url = "/lowcode/APP20210908194005415/live/?view=layui&fun=" + fun;
				return url;
			},
			//打开弹层
			openlayer: function (title, elem, successfunction, yesfunction, width, height, btn) {
				var self = this;
				layer.open({
					type: 2
					, title: title
					, content: elem
					, maxmin: true
					, area: [width, height]  //宽，高
					, btn: btn
					, success: function (layero, index) {
						if (successfunction) {
							successfunction();
						}
					}
					, btn2: function (index, layero) {
						$("#LAY-app-form-reset").trigger('click');
					}
					, cancel: function () {
						$("#LAY-app-form-reset").trigger('click');
					}
					, yes: function (index, layero) {
						self.data.layerindex = index;
						if (yesfunction) {
							if (getResult) {
								layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
								yesfunction(getResult());
							}
						}
					}
				});
			},
			//关闭弹层
			closelayer: function () {
				layer.close(this.data.layerindex);
			},
			initselect:function(){
				var self = this;
				var url = self.getbaseurl("LiveCourse.getcoursecategory");
				admin.req({
					type: 'get',
					url: url,
					done: function (obj) {
						console.log(obj)
						$.each(obj.data.data,function(index,item){
							$("#CategoryId").append(new Option(item.CategoryName,item.CategoryId))
						})
						layui.form.render("select")
					}
				});
			},
			//初始化事件
			initevent: function () {
				var self = this;
				self.initselect();


				self.data.datatableoptions = {
					elem: '#dataTable',
					where: {
						//token: layui.data(setter.tableName)['token'],
						CourseName: $("#CourseName").val(),  //获取文本值
						CategoryId: $("#CategoryId").val() //获取文本值
					},
					height: 'full-130',
					cellMinWidth: 80,
					size: 'sm',
					toolbar: '#toolbar',
					//url: '/lowcode/APP20210329185258876/BookManage/?fun=getbooks&view=layui',
					page: true,
					even: true,
					cols: [[
						{ width: "70", field: 'CourseId', title: 'CourseId', fixed: 'left', hide: true },
						{ width: "150", field: 'CourseName', title: '课程名称' },
						{ width: "150", field: 'CategoryName', title: '课程类型' },
						{ width: "200", field: 'CourseDesc', title: '课程介绍' },
						{
							width: "140", field: 'CoverImage', title: '课程封面', templet: function (d) {
								if (d.CoverImage) {
									return '<img src="' + d.CoverImage + ' " style="width: 40px; height: 40px;" />';
								} else {
									return ""
								}
							}
						},
						{ width: "90", field: 'DelFlag', title: '状态标识', align: 'center', templet: '#flagTpl' },
						{ width: "235", title: '操作', fixed: 'right', align: 'center', toolbar: '#option' }   //操作script的id                  
					]]
				}

				//查找reload,通过条件，查询网格的table重载数据显示到界面
				form.on("submit(LAY-app-front-search)", function (data) {
					self.searchbook(data);
				});

				//添加按钮的点击事件
				table.on('toolbar(dataTable)', function (obj) {
					console.log('table.on')
					switch (obj.event) {
						case 'add':
							console.log('add')
							self.addlivecourse();
							break;
					};
				});

				//监听行工具事件
				table.on('tool(dataTable)', function (obj) {  //tool是工具条事件名，table原始容器的属性
					var data = obj.data;//当前行数据
					var layEvent = obj.event;//获得lay-event的值
					if (obj.event === "edit") {
						self.editlivecourse(data);
					}
					if (obj.event === "selectDoc") {
						self.selectDoc(data)
					}
					// 启用停用机构
					if (obj.event === "disable" || obj.event === "enable") {

						var confirm_msg = "";
						var status = 0;
						if (data.DelFlag == 0) {
							confirm_msg = "确认停用么?";
							status = 1;
						}
						else {
							confirm_msg = "确认启用么?";
							status = 0;
						}

						layer.confirm(confirm_msg, { icon: 3, title: "提示" },
							function (index) {
								self.data.layerindex = index;
								self.deletecourse(data, status);
							});
					}
				});

			},
			//查询
			searchbook: function (formdata) {
				console.log(formdata)
				this.data.datatable.reload({
					where: {
						//token: layui.data(setter.tableName)['token'],
						CourseName: formdata.field.CourseName,
						CategoryId: formdata.field.CategoryId
					},
					page: {
						curr: 1
					}
				});
			},
			//加载课程列表
			getlivecourse: function () {
				var self = this;
				self.data.datatableoptions.url = self.getbaseurl("LiveCourse.getlivecourse");
				//表格绑定
				self.data.datatable = table.render(self.data.datatableoptions);
			},
			//添加
			addlivecourse: function () {
				var self = this;
				window.modelData = null;
				self.openlayer("新增课程", 'livecourseedit.html', null, function (field) {
					self.savelivecourse(field);
				}, '800px', '600px', ['保存', '取消']);
			},
			//编辑
			editlivecourse: function (row) {
				var self = this;
				window.modelData = row;
				self.openlayer("编辑课程", 'livecourseedit.html', null, function (field) {
					self.savelivecourse(field);
				}, '800px', '600px', ['保存', '取消']);
			},
			//授课医生
			selectDoc: function (row) {
				var self = this;
				window.modelData = row;
				self.openlayer("授课医生", 'livecoursedoctor.html', null, function(){self.closelayer()}, '100%', '100%', []);
			},
			//保存课程
			savelivecourse: function (field) {
				var self = this;
				var url = self.getbaseurl("LiveCourse.savelivecourse");
				admin.req({
					type: 'post',
					url: url,
					data: field,
					done: function (obj) {
						layer.msg("保存成功");

						self.data.datatable.reload(); //数据刷新
						self.closelayer(); //关闭弹层
					}
				});
			},
			//删除课程
			deletecourse: function (data, status) {
				var self = this;
				var url = self.getbaseurl("LiveCourse.deletecourse");
				admin.req({
					type: "post",
					url: url,
					data: {
						model: JSON.stringify(data),
						Status: status
					},
					done: function (res) {
						self.data.datatable.reload();// 数据刷新
						self.closelayer(); //关闭弹层
					}
				});
			}

		}

		/*
		* 入口
		*/
		controller.initpage();
	});

</script>